package com.bos.web.action;

import com.bos.domian.base.Area;
import com.bos.service.AreaService;
import com.bos.utils.PinYin4jUtils;
import com.bos.web.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author misterWei
 * @create 2018年11月13号:15点34分
 * @mailbox mynameisweiyan@gmail.com
 */
@ParentPackage("json-default")
@Namespace("/")
@Actions
@Controller
@Scope("prototype")
public class AreaAction extends BaseAction<Area> {

    @Autowired
    private AreaService areaService;
    //文件上传的核心
    private File file;           //文件元素
    private String fileFileName;//文件名称
    private String fileContentType;//文件类型

    public void setFile(File file) {
        this.file = file;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }

    @Action(value = "area_pageQuery", results = {@Result(type = "json")})
    public String areaPageQuery() {

        ActionContext.getContext().getValueStack().push(
                areaService.pageQuery(page - 1, rows, model));

        return SUCCESS;
    }

    @Action(value = "upload_Import", results = {@Result(location = "/pages/base/area.html")})
    public String uploadImport() {
        try {
            Workbook workbook = null;
            //两种方式，第一种xlsx 第二种xls 都可以兼容
            if (fileFileName.endsWith(".xls") || fileFileName.endsWith(".xlsx")) {
                //如果是2003版本
                if (fileFileName.endsWith(".xls")) {
                    //1.先解析文件
                    POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
                    workbook = new HSSFWorkbook(fs);
                } else if (fileFileName.endsWith(".xlsx")) {//如果是2007以上版本
                    workbook = new XSSFWorkbook(file);
                }

            }
            //创建List集合用来保存文件中的对象属性数据
            List<Area> areas = new ArrayList<Area>();
            //获取表格中第一个sheet
            Sheet sheetAt = workbook.getSheetAt(0);
            for (Row cells : sheetAt) {
                //第一页为行头数据我们不需要
                if (cells.getRowNum() == 0) {
                    continue;
                }
                String stringCellValue = cells.getCell(0).getStringCellValue();
                //如果遍历到最后一行，那么就会获取到一行空数据，那么就需要跳过逻辑的处理
                if (stringCellValue == null || "".equals(stringCellValue)) {
                    continue;
                }
                Area area = new Area();
                area.setProvince(cells.getCell(1).getStringCellValue());
                area.setCity(cells.getCell(2).getStringCellValue());
                area.setDistrict(cells.getCell(3).getStringCellValue());
                area.setPostcode(cells.getCell(4).getStringCellValue());
                area.setId(cells.getCell(0).getStringCellValue());
                //简码的生成
                String province = area.getProvince();
                province = province.substring(0, province.length() - 1);
                String[] headArray = PinYin4jUtils.getHeadByString(province);
                StringBuffer stringBuffer = new StringBuffer();
                for (String s : headArray) {
                    stringBuffer.append(s);
                }
                //生成简码
                area.setShortcode(stringBuffer.toString());
                //生成编码
                String[] strings = PinYin4jUtils.stringToPinyin(area.getCity().substring(0, area.getCity().length() - 1), false, "");
                StringBuffer citycode = new StringBuffer();
                for (String string : strings) {
                    citycode.append(string.trim());
                }
                area.setCitycode(citycode.toString());
                areas.add(area);
            }

            areaService.savaBatch(areas);
            ActionContext.getContext().getValueStack().push(true);
            return SUCCESS;
        } catch (Exception e) {
            ActionContext.getContext().getValueStack().push(false);
            e.printStackTrace();
            return SUCCESS;

        }

    }

}
